package com.ice.base.util;

import org.springframework.stereotype.Component;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 语音模块工具类
 */

@Component
public class VoiceUtil {

    /**
     * 发送语音内容
     * @param str
     */
    public void sendVoice(String ip, String port, String str){
        Socket socket = null;
        OutputStream os = null;
        BufferedWriter bw = null;
        try {
            socket = new Socket(InetAddress.getByName(ip), Integer.parseInt(port));
            socket.setSoTimeout(3);
            //2.得到socket读写流
            os = socket.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os, "GB2312"));
            //3.利用流按照一定的操作，对socket进行读写操作
            String info = "#" + str;
            bw.write(info);
            bw.flush();
            //延迟1秒关闭
            Thread.sleep(1000);
            //4.关闭资源
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("语音模块连接失败");
        }finally {
            try{
                if(null != socket){
                    socket.close();
                }
                if(null != bw){
                    bw.close();
                }
                if(null != os){
                    os.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }

        }
    }


}